package com.duke.payphone.sell;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.duke.payphone.Global;
import com.duke.payphone.LoginActivity;
import com.duke.payphone.R;
import com.duke.payphone.ServerPortal;
import com.duke.payphone.factory.MapFactory;


/**
 * activity responsible for displaying transaction summary at the end
 * activity will pull transaction info from globalvars to populate this screen
 * -product name, date of transaction, price, buyer, seller, [picture]
 * 
 * button: okay (to ROOT)
 * 
 * @author Michael
 * 
 */
public class SellCompleteActivity extends Activity {

    private Global globalvars;
    
    private TextView productname;
    private TextView date;
    private TextView price;
    private TextView seller;
    private TextView buyer;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sell_complete);

        globalvars = (Global) getApplicationContext();
        
        productname = (TextView) findViewById(R.id.tv_productname);
        date = (TextView) findViewById(R.id.tv_date);
        price = (TextView) findViewById(R.id.tv_price);
        seller = (TextView) findViewById(R.id.tv_seller);
        buyer = (TextView) findViewById(R.id.tv_buyer);

        Intent i = getIntent();
        productname.setText(i.getExtras().getString("productname"));
        date.setText(i.getExtras().getString("date"));
        price.setText(i.getExtras().getString("price"));
        seller.setText(i.getExtras().getString("seller"));
        buyer.setText(i.getExtras().getString("buyer"));
    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu) {
        // getMenuInflater().inflate(R.menu.activity_sell_complete, menu);
        // return true;
        return false;
    }

//    public void executeDone (View v) {
//        finish();
//    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * @param v
     */
    public void executeDone (View v) {

        // check validity
        if (true) {

            // send to server
            Map<String, Serializable> response = ServerPortal.contactServer(prepareTransmitMap());

            // wait for reply
            if (response.containsKey("server") && response.get("server").equals("valid")) {

                if (response.containsKey("session") && response.get("session").equals("valid")) {

                    if (response.containsKey("result") && response.get("result").equals("valid")) {
                        onSellNewValid(response);
                    }
                    else {
                        onSellNewInvalid(response);
                    }
                }
                else {
                    onSessionInvalid();
                }
            }
            else {
                onServerInvalid();
            }
        }
    }


    /**
     * returns a map
     * 
     * @return
     */
    private Map<String, Serializable> prepareTransmitMap () {
        Map<String, Serializable> request = MapFactory.createTransmitMap("sellcomplete");

        return request;
    }


    /**
     * @param response
     */
    private void onSellNewValid (Map<String, Serializable> response) {
        Toast.makeText(globalvars, "Transaction database updated.", Toast.LENGTH_SHORT)
        .show();
        finish();
    }

    /**
     * @param response
     */
    private void onSellNewInvalid (Map<String, Serializable> response) {
        Toast.makeText(globalvars, "Unable to complete transaction. Please try again.", Toast.LENGTH_SHORT)
                .show();
    }

    /**
     * If server has timed out
     */
    private void onServerInvalid () {
        // toast: server error, please verify server connection and retry
        // request
        Toast.makeText(globalvars, "Server/network error. Please retry.", Toast.LENGTH_SHORT)
                .show();
    }

    /**
     * If session has timed out
     */
    private void onSessionInvalid () {
        // reset the sessionID to null
        Global.sessionid = "";

        // toast: current session has timed out
        Toast.makeText(globalvars, "Current session has timed out.", Toast.LENGTH_SHORT).show();

        // go back to the login screen
        Intent toLogin = new Intent(this, LoginActivity.class);
        toLogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(toLogin);
        finish();
    }
    
    
    
    
    

}
